package com.pm2.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pm2.dao.meeting.MeetingDao;
import com.pm2.dao.meeting.MeetingDetailDao;
import com.pm2.dao.plan.MasterPlanDao;
import com.pm2.entity.plan.MasterPlan;
import com.pm2.entity.weekMeet.Meeting;
import com.pm2.entity.weekMeet.MeetingDetail;
import com.pm2.entity.weekMeet.TaskAttachmentVo;
import com.pm2.entity.weekMeet.enumerate.SettleAccountsStatus;
import com.pm2.entity.weekMeet.enumerate.TaskStatus;
import com.pm2.service.meeting.MeetingService;
import com.shuohe.util.date.SDate;
import com.shuohe.util.returnBean.ReturnBean;

@Service
public class MeetingServiceImpl implements MeetingService{

	@Autowired
	private MeetingDao meetingDao; 
	
	@Autowired
	private MeetingDetailDao meetingDetailDao; 
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Autowired
	private MasterPlanDao masterPlanDao; 
	
	@Override
	@Modifying
	@Transactional
	public ReturnBean save(Meeting meeting, List<MeetingDetail> list) {
		// TODO Auto-generated method stub
		
		if(meeting == null) return new ReturnBean(false,"会议主表不能为空");
		try
		{
			Meeting m = meetingDao.save(meeting);
			List<MeetingDetail> l = new ArrayList<MeetingDetail>();
			for(MeetingDetail d:list)
			{
				MeetingDetail md = d;
				md.setPid(m.getId());
				md.setPerformance_status(SettleAccountsStatus.NOFINISH.toString());
				md.setTask_status(TaskStatus.nofinish.toString());
				md.setEstimate_status("0");
//				BigDecimal n = new BigDecimal(0);
//				md.setEstimate_coefficient(n);
				l.add(md);
			}
			meetingDetailDao.save(l);
			return new ReturnBean(true,"保存成功");
		}catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,"保存失败");
		}
	}

	@Override
	@Modifying
	@Transactional
	public ReturnBean update(Meeting meeting, List<MeetingDetail> list) {
		if(meeting == null) return new ReturnBean(false,"会议主表不能为空");
		try
		{
			//删除原字表
			meetingDetailDao.deleteByPid(meeting.getId());
			
			Meeting m = meetingDao.save(meeting);
			List<MeetingDetail> l = new ArrayList<MeetingDetail>();
			for(MeetingDetail d:list)
			{
				MeetingDetail md = d;
				md.setPid(m.getId());
				md.setPerformance_status(SettleAccountsStatus.NOFINISH.toString());
				md.setTask_status(TaskStatus.nofinish.toString());
				md.setEstimate_status("0");
				l.add(md);
			}
			meetingDetailDao.save(l);
			return new ReturnBean(true,"保存成功");
		}catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,"保存失败");
		}
	}

	@Override
	@Modifying
	@Transactional
	public ReturnBean delete(int id) {
		Meeting m = meetingDao.findById(id);
		if(m == null) return new ReturnBean(false,"不存在的会议ID");
		
		try
		{
			meetingDao.deleteById(id);
			meetingDetailDao.deleteByPid(id);	
			return new ReturnBean(true,"删除成功");
		}catch(Exception e)
		{
			e.printStackTrace();
			return new ReturnBean(false,"删除失败");
		}		
	}

	@Override
	public void taskEstimate(int master_plan_id,String finish_status, String finish_situation, String task_response) {
		// TODO Auto-generated method stub
		MeetingDetail md = meetingDetailDao.findOne(master_plan_id);
		//根据难度等级和完成情况获取评估系数
		String sql = "select * from `db_pm2_work_difficulty_ coefficient` "
				+ " WHERE difficulty_level = '"+md.getDifficulty_level()+"' "
				+ " and finish_situation='"+finish_situation+"'";
		Map<String,Object> map = jdbcTemplate.queryForMap(sql);
		BigDecimal estimate_coefficient  = (BigDecimal) map.get("praise_punish_coefficient");
		md.setEstimate_coefficient(estimate_coefficient);
		md.setFinish_situation(finish_situation);
		md.setEstimate_describe(task_response);
		md.setFinish_stutus(finish_status);
		md.setEstimate_status("1");
		meetingDetailDao.save(md);
		System.out.println(map);
		
	}

	@Override
	public void taskFeedback(int master_plan_id, String finish_describe) {
		// TODO Auto-generated method stub
		MeetingDetail md = meetingDetailDao.findOne(master_plan_id);
		md.setFinish_describe(finish_describe);
		md.setFinish_time(SDate.getSystemDateToString());
		md.setTask_status(TaskStatus.finish.toString());
		meetingDetailDao.save(md);
		//任务完成之后将主计划中的任务状态标识为已经完成
		Integer plan_id = md.getPlan_id();
		Integer level = md.getTask_level();
		if(null!=plan_id) {
			MasterPlan mp = masterPlanDao.findOne(plan_id);
			if(3==level) {
				mp.setTask_level_3_state("1");
			}else if(2==level) {
				mp.setTask_level_2_state("1");
			}else if(1==level) {
				mp.setTask_level_1_state("1");
			}
			masterPlanDao.save(mp);
		}
		
		
	}

}
